/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fxml_sales;


import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import com.db.DashBoardDao;
import com.to.CategoryTo;
import com.to.ResultTo;
import com.to.ValueTo;
import java.util.ArrayList;

import javafx.scene.control.Slider;
import javafx.scene.text.FontWeight;


public class SampleController implements Initializable {
    BorderPane mainPane = new BorderPane(); // This is the main layout in the dash board
    
    @FXML
    HBox tophbox = new HBox(); // this is the top part of the dash board
    
    @FXML
    final CategoryAxis xAxis = new CategoryAxis(); // used in the bar chart for the category represents the names of category
    @FXML
    final NumberAxis yAxis = new NumberAxis(); // used in the bar chart for the category represents the sales amount
    
    @FXML
    BarChart<String,Number> salesChart = new BarChart<String,Number>(xAxis,yAxis); // the bar chart for the product category
    
    @FXML
    private TableView<ResultTo> main_table = new TableView<ResultTo>(); // this is the table represent the product category sales data
    @FXML
    private IntegerProperty index = new SimpleIntegerProperty(); // used to get the table row index
    
    
    @FXML
    private TableView<ResultTo> table2 =  new TableView<ResultTo>(); // this is the table represent the product sales data
    @FXML
    PieChart piechart = new PieChart();  // pie chart for the product on selection of a category in the main_table
    @FXML
    final Label main_table_label = new Label("Categories Sales Details");  // head label in the main_table
    @FXML
    final Label table2_label = new Label("Products Sales Details"); // head label in the table2(product sale detail table)
    @FXML
    final Label top_label = new Label("DashBoard"); // label shows in the header of the dash board, the top banner
    
    // Components used in second tab of Dash board
    @FXML
    VBox mainVbox = new VBox(); // main container of tab 2
    @FXML
    HBox filterbox = new HBox(); // horizontal box where the filters ( period and category combo box ) are present
    @FXML
    HBox graphBox = new HBox();  // area chart horizontal box
    @FXML
    final Label peiod = new Label("Period :");
    @FXML
    final Label category = new Label("Product Category :");
    
    @FXML
    PieChart mark_pie_chart = new PieChart(); // pie chart in the second tab for marketing cost for a category's products
    @FXML
    ComboBox<CategoryTo> catCombo = new ComboBox<CategoryTo>();  // category combo box in tab2
    ComboBox<String> periodCombo = null; // period combo box in tab2
    @FXML
    private TableView<ResultTo> table3 =  new TableView<ResultTo>(); // table in the tab2
    
    @FXML
    HBox dashBox3 = new HBox();
    @FXML
    final CategoryAxis xAxis1 = new CategoryAxis();
    
    @FXML
    final NumberAxis yAxis1 = new NumberAxis();
    @FXML
    final LineChart<String,Number>   ac =  new LineChart<String,Number>(xAxis1,yAxis1);
    
    DashBoardDao dao = new DashBoardDao();
    
    //Optimize marketing spend components TAB3
    @FXML
    private TableView<ResultTo> main_table_dash3 = new TableView<ResultTo>();
    @FXML
    PieChart allCatPie = new PieChart();
    @FXML
    ComboBox<CategoryTo> catCombo2 = new ComboBox<CategoryTo>();
    @FXML
    Slider slider = new Slider(0, 100, 0);
   
    
    @FXML
    VBox tab3Vbox = new VBox();
     @FXML
    VBox tab3Vbox2 = new VBox();
     @FXML
     HBox tab3_main_hbox = new HBox();
    @FXML
    HBox tab3Hbox1 = new HBox();
    @FXML
    HBox tab3Hbox2 = new HBox();
    @FXML
    HBox tab3Hbox3 = new HBox();
    
    @FXML
    Label tab3Label1 = new Label("Select a Category :");
    @FXML
    Label tab3Label2 = new Label("Mode :");
    @FXML
    Label tab3Label3 = new Label("Mrkt Spend Slider");
    
    @FXML
    ToggleGroup tgroup = new ToggleGroup();
    @FXML
    RadioButton rb1 = new RadioButton("Increase");
    @FXML
    RadioButton rb2 = new RadioButton("Decrease");
    
    
     
 @FXML
    final CategoryAxis xAxis2 = new CategoryAxis(); // used in the bar chart for the category represents the names of category
    @FXML
    final NumberAxis yAxis2 = new NumberAxis(); // used in the bar chart for the category represents the sales amount    
    
    @FXML
    private TableView<ValueTo> tab3_val_table = new TableView<ValueTo>();
    @FXML
        BarChart<String,Number> targetChart = new BarChart<String,Number>(xAxis2,yAxis2);
    
XYChart.Series<String,Double> barSeries = dao.getTargetBarChart();
 XYChart.Series series2 = dao.getTargetBarChart();
//XYChart.Series<String,Double> newSaleBarSeries = new XYChart.Series<String,Double>();
XYChart.Series<String,Double> newSaleBarSeries = null;
ObservableList<ResultTo> tab3_table_data = dao.getCatResult();

ObservableList<ResultTo> tab3_table_new = FXCollections.observableArrayList();
ObservableList<ResultTo> temp = null;
ArrayList<Double> newMrktList = new ArrayList<Double>();
ArrayList<Double> newSaleList = null;
double newMrktingVal = 0.0;
double newSalesVal = 0.0;

 XYChart.Series series1 = dao.getBarChart();
 
public ObservableList<ResultTo> reCreateTable(String cat,String mode,String sliderVal){
    
    int slider_val = sliderValue(new Double(sliderVal));
    
    System.out.println("Slider change value = " + slider_val + "********************");
    double per = getPercentage(cat);
     temp = FXCollections.observableArrayList();
    for(ResultTo to : tab3_table_data){
       
        if(cat.equalsIgnoreCase(to.getDesc())){
            int index = tab3_table_data.indexOf(to);
            if("INCREASE".equalsIgnoreCase(mode)){
               newMrktingVal = to.getMrktCost() +  (slider_val*10000);
               newSalesVal = to.getAmount() * (1 + per*(slider_val/10));
            }else{
                newMrktingVal = to.getMrktCost() -  (slider_val*10000);
                 newSalesVal = to.getAmount() * (1 - per*(slider_val/10));
            }
            to.setNewMrktCost(newMrktingVal);
            to.setNewSale(newSalesVal);
            // tab3_table_new.remove(index);
             
        }
       temp.add( to);
      
    }
    tab3_table_data = FXCollections.observableArrayList();
    for(ResultTo to : temp){
        tab3_table_data.add(to);
    }
   
    return tab3_table_data ;
}

    // this method initialize all the components in the dash board
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        populate_salesBarChart();
        System.out.println("Tab1 : bar chart initialized ");
        populateMainTable();
        System.out.println("Tab1 :Main table initialized ");
        table2.setVisible(false);
        piechart.setVisible(false);
        table2_label.setVisible(false);
        main_table_label.setFont(new Font(18));
        table2_label.setFont(new Font(18));
        getFilterHBox();
        System.out.println("Tab2 : Filter box initialized ");
        populate_markPieChart(catCombo.getValue().toString(), periodCombo.getValue().toString());
        System.out.println("Tab2 : pie chart initialized ");
        populateTable3(catCombo.getValue().toString(), periodCombo.getValue());
        System.out.println("Tab2 : table 3 initialized ");
        genDash3(catCombo.getValue().toString(), periodCombo.getValue().toString());
        System.out.println("Tab2 : area chart initialized ");
        //------Tab 3 components initiatialization
        reCreateTable("Breverages","INCREASE","0");
        reCreateBarGraph();
        populateDash3MainTable();
        populate_allCatPie();
        createSlider();
        getFilterBox();
        populate_targetBarChart(series1,series2,newSaleBarSeries);
       
     }    
    
    public void reCreateBarGraph(){
        newSaleBarSeries = new XYChart.Series<String,Double>();
        for(ResultTo to : tab3_table_data){
            newSaleBarSeries.getData().add(new XYChart.Data(to.getDesc(),to.getNewSale()));
        }
    }
   
    /**
     * Populate the product category bar chart in the tab1 of the dash board
     */
    
     @SuppressWarnings({ "rawtypes", "unchecked" })
  private void populate_salesBarChart(){
        salesChart.setTitle("Total Sales Summary");
        xAxis.setLabel("<----- Product Category ----- >");       
        yAxis.setLabel("<---- Sales in $ (K)---->");
        XYChart.Series series1 = dao.getBarChart(); // get the data from DAO method (DB)
        series1.setName("Product Categories");
        salesChart.setBarGap(2);
        salesChart.maxWidth(200);
        salesChart.getData().addAll(series1);
        System.out.println("Tab1: Bar chart is created...");
    }
     
     /**
      * Populate the product category table (main_table) in the tab1 of the dash board
      */
     
     
     @SuppressWarnings({ "unchecked", "rawtypes" })
    private void populateMainTable() {
        main_table.setMaxHeight(300);
        main_table.setMaxHeight(475);
        main_table.getItems().clear();
        dao = new DashBoardDao();
        main_table.setItems(dao.getCatResult()); // get the data from DAO method (DB)
        
        // first column(Category Name) of the main_table
        TableColumn<ResultTo, String> category_name = new TableColumn<ResultTo, String>("Category Name");
        category_name.setMinWidth(170);
        category_name.setCellValueFactory(new PropertyValueFactory<ResultTo, String>("desc"));
        
        //second column(% of Total Sale) of the main_table
        TableColumn<ResultTo, Double> percentage = new TableColumn<ResultTo, Double>("% of Totalsale");
        percentage.setMinWidth(150);
        percentage.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("percentage"));
        
        //Third column(Total Sale in $) of the main_table
        TableColumn<ResultTo, Double> total_sale = new TableColumn<ResultTo, Double>("Total Sale (in $ K)");
        total_sale.setMinWidth(150);
        total_sale.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("amount"));
        
        
        // adding all the columns in the table
        main_table.getColumns().setAll(category_name,percentage, total_sale);
        main_table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        indexProperty().addListener(new ChangeListener() {
                 
            @Override
            public void changed(ObservableValue o, Object oldVal,
                    Object newVal) {
                System.out.println("Index has changed!" +newVal );
            }
        });
        
        // generate the dynamic Pie chart and table for the products in a category in tab1
        main_table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
 
            @Override
            public void changed(ObservableValue observable, Object oldvalue, Object newValue) {
                ResultTo selectedTo = (ResultTo) newValue;
                setIndex(dao.getCatResult().indexOf(newValue));
                populate_prodPieChart(selectedTo.getDesc());
                populateTable2(selectedTo.getDesc());
            }
        });
        System.out.println("Tab1: Main table is created...");
   }
     
     /**
      * Populate the product  table (table2) in the tab1 of the dash board
      */
     
      @SuppressWarnings({ "unchecked" })
      private void populateTable2(final String cat) {
  table2_label.setVisible(true);
  table2.setVisible(true);
 table2.setMaxHeight(220);
 table2.setMaxWidth(450);
 table2.getItems().clear();
 dao = new DashBoardDao();
  table2.setItems(dao.getProdResult(cat)); // get the data from DAO method (DB)
 TableColumn<ResultTo, String> product_name = new TableColumn<ResultTo, String>("Product Name");
 product_name.setMinWidth(150);
 product_name.setCellValueFactory(new PropertyValueFactory<ResultTo, String>("desc"));

 TableColumn<ResultTo, Double> total_sale = new TableColumn<ResultTo, Double>("Total Sale (in $K)");
 total_sale.setMinWidth(145);
  total_sale.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("amount"));

 TableColumn<ResultTo, Double> percentage = new TableColumn<ResultTo, Double>("% of Totalsale");
 percentage.setMinWidth(145);
  percentage.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("percentage"));

table2.getColumns().setAll(product_name,percentage, total_sale);
 table2.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
                System.out.println("Tab1: Table2 is created...");
      }
      
     /**
      *  Populate the pie chart for the category having diff products in tab1
      */
      @SuppressWarnings("unchecked")
      private void populate_prodPieChart(final String category){
        piechart.setVisible(true);
        piechart.setTitle("Products Revenue (%)in the Category");
        
        piechart.setData(dao.getDataCatwise(category)); // get the data from DAO method (DB)
        System.out.println("Tab1: pie chart is created...");
      }
      
     /**
      *  Generate the pie chart for the marketing cost in each category in tab2
      */
     @SuppressWarnings("unchecked")
     private void populate_markPieChart(final String category, final String quater){
   mark_pie_chart.setTitle("Marketing cost in the Category");
    mark_pie_chart.setData(dao.getMarketingDataCatwise(category,quater)); // get the data from DAO method (DB)
            System.out.println("Tab2: pie chart is created...");
     }
     
     /**
      * Create the category combo box in tab2
      */
     private void getCatCombobox(){
        dao = new DashBoardDao();
        ObservableList<CategoryTo> data = FXCollections.observableArrayList(dao.getAllProductCat());
        catCombo  = new ComboBox<>(data);
        catCombo.getSelectionModel().selectFirst();
        System.out.println("Category Combo created");
        catCombo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<CategoryTo>() {
        // call this method when the combo box value changed
            @Override
            public void changed(ObservableValue<? extends CategoryTo> arg0, CategoryTo arg1, CategoryTo arg2) {
                if (arg2 != null) {
                    System.out.println("Selected employee: " + arg2.getDesc());
                    populate_markPieChart(arg2.getDesc(),periodCombo.getValue().toString());
                    populateTable3(arg2.getDesc(),periodCombo.getValue().toString());
                    genDash3(arg2.getDesc(),periodCombo.getValue().toString());
                }
            }
        });
    }
     
     
     
     /**
      * This method is used for generating the Period combo box in the tab 2
      *
      */
     
     private void getPeriodCombo(){
         ObservableList<String> data = FXCollections.observableArrayList(dao.getPeriod());
         periodCombo  = new ComboBox<>(data);
         periodCombo.getSelectionModel().selectFirst();
         periodCombo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {

            @Override
            public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
                if (arg2 != null) {
                    System.out.println(catCombo.getValue());
                    populate_markPieChart(catCombo.getValue().toString(),arg2);
                    populateTable3(catCombo.getValue().toString(),arg2);
                    genDash3(catCombo.getValue().toString(),arg2);
                }
            }
        });
     }
     
     
     /**
      * This method generate the first HBox for the tab2 2 in the Vbox.
      * Here the components to be added are the labels and the combo box for the filter of the results
      */
     
     private void getFilterHBox(){
         peiod.setFont(new Font("System Bold", 13));
         category.setFont(new Font("System Bold", 13));
         getCatCombobox();
         getPeriodCombo();
         filterbox.getChildren().addAll(category,catCombo,peiod,periodCombo);
     }
     
     /**
      * This method get the month number from month name
      */
    
     private int getMonth(String select){
         String month = select.substring(0,3);
         int mon=0;
         if ("JAN".equalsIgnoreCase(month))
             mon = 1;
         else if ("FEB".equalsIgnoreCase(month))
             mon = 2;
         else if ("MAR".equalsIgnoreCase(month))
             mon = 3;
         else if ("APR".equalsIgnoreCase(month))
             mon = 4;
         else if ("MAY".equalsIgnoreCase(month))
             mon = 5;
         else if ("JUN".equalsIgnoreCase(month))
             mon = 6;
         else if ("JUL".equalsIgnoreCase(month))
             mon = 7;
         else if ("AUG".equalsIgnoreCase(month))
             mon = 8;
         else if ("SEP".equalsIgnoreCase(month))
             mon = 9;
         else if ("OCT".equalsIgnoreCase(month))
             mon = 10;
         else if ("NOV".equalsIgnoreCase(month))
             mon = 11;
         else if ("DEC".equalsIgnoreCase(month))
             mon = 12;
         
        return mon;
     }
    
     /**
      * Get the year from the period combo box selection
      */
     private int getYear(String select){
         return new Integer(select.substring(6,10));
     }

     /**
      * Populate the product category marketing cost (table3) in the tab2 of the dash board
      */
     
     @SuppressWarnings({ "unchecked"})
        private void populateTable3(final String cat,final String quater) {
          
        table3.setMaxHeight(220);
        table3.setMaxWidth(540);
        table3.getItems().clear();
        dao = new DashBoardDao();
        table3.setItems(dao.getMarketingCatWise(cat,quater)); // get the data from DAO method (DB)
        TableColumn<ResultTo, String> product_name = new TableColumn<ResultTo, String>("Product Name");
        product_name.setMinWidth(175);
        product_name.setCellValueFactory(new PropertyValueFactory<ResultTo, String>("desc"));
        
        TableColumn<ResultTo, Double> markt_cost = new TableColumn<ResultTo, Double>("Marketing Cost (in $)");
        markt_cost.setMinWidth(155);
        markt_cost.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("amount"));
        
        TableColumn<ResultTo, Double> percentage = new TableColumn<ResultTo, Double>("% of Marketing Cost");
        percentage.setMinWidth(190);
        percentage.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("percentage"));
        
        table3.getColumns().setAll(product_name,percentage, markt_cost);
        table3.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    }
     
     /**
      *  this method generate the area chart in tab2
      */
     @SuppressWarnings({ "rawtypes", "unchecked" })
 private void genDash3(final String catName,final String quater){
     XYChart.Series seriesSale= dao.getAreaSaleChart(catName,quater);
        XYChart.Series seriesMrkt= dao.getAreaMarketChart(catName,quater);
        seriesSale.setName("Total Sales ");
        seriesMrkt.setName("Marketing Cost");
        ac.getData().clear();
        ac.getData().addAll(seriesSale, seriesMrkt);
   }
     
   @SuppressWarnings({ "unchecked", "rawtypes" })
    private void populateDash3MainTable() {
        main_table_dash3.setMaxHeight(300);
        main_table_dash3.setMaxHeight(620);
        main_table_dash3.getItems().clear();
        dao = new DashBoardDao();
        
        main_table_dash3.setItems(tab3_table_data); // get the data from DAO method (DB)
        
        // first column(Category Name) of the main_table
        TableColumn<ResultTo, String> category_name = new TableColumn<ResultTo, String>("Category Name");
        category_name.setMinWidth(170);
        category_name.setCellValueFactory(new PropertyValueFactory<ResultTo, String>("desc"));
        
        //second column(% of Total Sale) of the main_table
        TableColumn<ResultTo, Double> percentage = new TableColumn<ResultTo, Double>("% of Totalsale");
        percentage.setMinWidth(150);
        percentage.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("percentage"));
        
        //Third column(Total Sale in $) of the main_table
        TableColumn<ResultTo, Double> total_sale = new TableColumn<ResultTo, Double>("Total Sale (in $ K)");
        total_sale.setMinWidth(150);
        total_sale.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("amount"));
      
        TableColumn<ResultTo, Double> target = new TableColumn<ResultTo, Double>("Target");
        
        
        target.setMinWidth(150);
        target.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("target"));
        //target.setCellFactory(getCustomCellFactory("red"));
        
        /*category_name.setCellFactory(new Callback<TableColumn<ResultTo, String>, TableCell<ResultTo, String>>() {
            public TableCell call(TableColumn param) {
            	
                return new TableCell<ResultTo, String>() {

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!isEmpty()) {
                            this.setTextFill(javafx.scene.paint.Color.RED);
                            
                            
                            // Get fancy and change color based on data
                            if(item.contains("@")) 
                                this.setTextFill(javafx.scene.paint.Color.BLUEVIOLET);
                            setText(item);
                        }
                    }
                };
            }
        });*/
        
         TableColumn<ResultTo, Double> markt_cur = new TableColumn<ResultTo, Double>("Marketing Cost");
        markt_cur.setMinWidth(150);
        markt_cur.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("mrktCost"));
        
        TableColumn<ResultTo, Double> markt_new = new TableColumn<ResultTo, Double>("New Marketing Cost");
        markt_new.setMinWidth(150);
        markt_new.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("newMrktCost"));
        
        TableColumn<ResultTo, Double> rev_new = new TableColumn<ResultTo, Double>("New Sale");
        rev_new.setMinWidth(150);
        rev_new.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("newSale"));
        
        // adding all the columns in the table
        main_table_dash3.getColumns().setAll(category_name, markt_cur,markt_new,total_sale,target,rev_new);
        main_table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        
   }
   
   private Callback<TableColumn<ResultTo, Double>, TableCell<ResultTo, Double>> getCustomCellFactory(final String color) {
       return new Callback<TableColumn<ResultTo, Double>, TableCell<ResultTo, Double>>() {

           @Override
           public TableCell<ResultTo, Double> call(TableColumn<ResultTo, Double> param) {
               TableCell<ResultTo, Double> cell = new TableCell<ResultTo, Double>() {
            	   public void updateItem(final String item, boolean empty) {
            		   if (item != null) {
                           setText(item);
                           setStyle("-fx-text-fill: " + color + ";");
                       }
            	   }
                  
               };
               return cell;
           }
       };
   }
   @SuppressWarnings("unchecked")
      private void populate_allCatPie(){
       
        allCatPie.setTitle("Category");
        allCatPie.setData(dao.getAllCatPie()); // get the data from DAO method (DB)
        System.out.println("Tab3: pie chart is created...");
      }  
     private void getCatCombobox2(){
        dao = new DashBoardDao();
        ObservableList<CategoryTo> data = FXCollections.observableArrayList(dao.getAllProductCat());
        catCombo2  = new ComboBox<>(data);
        catCombo2.getSelectionModel().selectFirst();
        System.out.println("Category Combo2 created");
        catCombo2.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<CategoryTo>() {
        // call this method when the combo box value changed
            @Override
            public void changed(ObservableValue<? extends CategoryTo> arg0, CategoryTo arg1, CategoryTo arg2) {
                if (arg2 != null) {
                    System.out.println("Selected employee: " + arg2.getDesc());
                    
                    reCreateTable( arg2.getDesc(),tgroup.getSelectedToggle().getUserData().toString(),new Double(slider.getValue()).toString());
                   populateDash3MainTable();
                /*  reCreateBarGraph();
                    populate_targetBarChart(series1,series2,newSaleBarSeries);*/
                   
                    
                }
            }
        });
    }
     
    private void createSlider(){
        slider.setMajorTickUnit(10);
        slider.minWidth(400);
        slider.setMinorTickCount(0);
        slider.setShowTickLabels(true);
        slider.setShowTickMarks(true);
    }
     
     private void getFilterBox(){
          getCatCombobox2();
          rb1.setToggleGroup(tgroup);
          rb1.setUserData("INCREASE");
          rb1.setSelected(true);
          rb2.setUserData("DECREASE");
          rb2.setToggleGroup(tgroup);
          
          tgroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
              public void changed(ObservableValue<? extends Toggle> ov,
                  Toggle old_toggle, Toggle new_toggle) {
                      if (tgroup.getSelectedToggle() != null) {
                      
           reCreateTable(catCombo2.getValue().getDesc(),tgroup.getSelectedToggle().getUserData().toString(),new Double(slider.getValue()).toString());
                          populateDash3MainTable();
                          /*reCreateBarGraph();
                           targetChart.getData().removeAll();
                          populate_targetBarChart(series1,series2,newSaleBarSeries);*/
                  }                
              }
          });
          
          
          
          slider.valueProperty().addListener(new ChangeListener<Number>() {
              public void changed(ObservableValue<? extends Number> ov,
                  Number old_val, Number new_val) {
                     reCreateTable(catCombo2.getValue().getDesc(),tgroup.getSelectedToggle().getUserData().toString(),new_val.toString());
                      populateDash3MainTable();
                     //tab3Hbox1.getChildren().remove(main_table_dash3);
                     //tab3Hbox1.getChildren().add(main_table_dash3);
                     System.out.println("Table re Created **********");
                     /* reCreateBarGraph();
                           targetChart.getData().removeAll();
                      populate_targetBarChart(series1,series2,newSaleBarSeries);*/
          
              }
          });
          
         tab3Hbox1.getChildren().addAll(main_table_dash3);
          tab3Label1.setFont(Font.font("Arial", FontWeight.BOLD, 14));
          tab3Label2.setFont(Font.font("Arial", FontWeight.BOLD, 14));
          tab3Label3.setFont(Font.font("Arial", FontWeight.BOLD, 14));
         tab3Hbox2.getChildren().addAll(tab3Label1,catCombo2,tab3Label2, rb1,rb2,tab3Label3,slider);
         //populateValTable();
     }
     
     
     
    
     
    
   
    
      @SuppressWarnings({ "rawtypes", "unchecked" })
  private void populate_targetBarChart(XYChart.Series series1,XYChart.Series series2,XYChart.Series series3){
         
          
        targetChart.setTitle("Sales vs Target");
        xAxis2.setLabel("<----- Product Category ----- >");       
        yAxis2.setLabel("<----  in $ ---->");
        // get the data from DAO method (DB)
        series1.setName("Sale");
       // get the data from DAO method (DB)
        series2.setName("Target");
       // XYChart.Series series3 = dao.getMrktBarChart(); // get the data from DAO method (DB)
        series3.setName("Change");
        targetChart.setBarGap(2);
        targetChart.maxWidth(200);
        targetChart.getData().addAll(series1,series2,series3);
        System.out.println("Tab1: Bar chart is created...");
    }
      
    public int sliderValue(double val){
        int newVal = 0;
        if(val >0 && val <= 10){
            newVal =10;
        }else if(val > 10 && val <=20){
            newVal =20;
        }
        else if(val > 20 && val <=30){
            newVal =30;
        }
        else if(val > 30 && val <=40){
            newVal =40;
        }
        else if(val > 40 && val <=50){
            newVal =50;
        }
        else if(val > 50 && val <=60){
            newVal =60;
        }
        else if(val > 60 && val <=70){
            newVal =70;
        }
        else if(val > 70 && val <=80){
            newVal =80;
        }else if(val > 80 && val <=90){
            newVal =90;
        }else if(val > 90 && val <=100){
            newVal =100;
        }
        
        
        return newVal;
    }
    
    public double getPercentage(String cat){
        double per = 0.0;
        if("Beverages".equalsIgnoreCase(cat)){
            per = 0.08;
        }else if("Cereal".equalsIgnoreCase(cat)){
            per = 0.10;
        }else if("Coffee".equalsIgnoreCase(cat)){
            per = 0.2;
        }else if("Confectionary".equalsIgnoreCase(cat)){
            per = 0.005;
        }else if("Drinks".equalsIgnoreCase(cat)){
            per = 0.02;
        }else if("Formula".equalsIgnoreCase(cat)){
            per = 0.12;
        }else if("Frozen Foods".equalsIgnoreCase(cat)){
            per = 0.01;
        }else if("Ice Cream".equalsIgnoreCase(cat)){
            per = 0.05;
        }else if("Baby Food".equalsIgnoreCase(cat)){
            per = 0.07;
        }else if("Yogurts".equalsIgnoreCase(cat)){
            per = 0.03;
        }
        
        return per;
    }
     
     public final double getIndex() {
        return index.get();
    }
 
    
    public final void setIndex(Integer value) {
        index.set(value);
    }
 
    
    public IntegerProperty indexProperty() {
        return index;
    }
}

